﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using Microhard.WarehouseInsp.Buz.Models;
using Microhard.WarehouseInsp.Buz.Msg;
using Microhard.WarehouseInsp.Buz.Interfaces;
using Microhard.Core;
using FluentValidation.Results;

namespace Microhard.WarehouseInsp.Buz.Validator
{

    public class InspectionRecordModelValidator : AbstractValidator<InspectionRecordModel>
    {
        public InspectionRecordModelValidator()
        {
            RuleSet("Insert", () =>
            {
                RuleFor(u => u.InsEmployeeName).NotEmpty().WithMessage(InspectionRecordMsg.NeedInsEmployeeName).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.InsEmployeePhone).NotEmpty().WithMessage(InspectionRecordMsg.NeedInsEmployeePhone).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.InsEmployeeName).Length(1, 20).WithMessage(InspectionRecordMsg.MaxNameLength).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                RuleFor(u => u.PrjName).NotEmpty().WithMessage(InspectionRecordMsg.NeedPrjName).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                Custom(u =>
                {
                    if (Guid.Empty == u.WarehouseID)
                    {
                        return new ValidationFailure("WarehouseID", InspectionRecordMsg.NeedWarehouseID);
                    }
                    return null;
                });

            });

            RuleSet("Update", () =>
            {
                RuleFor(u => u.InsEmployeeName).NotEmpty().WithMessage(InspectionRecordMsg.NeedInsEmployeeName);
                RuleFor(u => u.InsEmployeePhone).NotEmpty().WithMessage(InspectionRecordMsg.NeedInsEmployeePhone);
                RuleFor(u => u.InsEmployeeName).Length(1, 20).WithMessage(InspectionRecordMsg.MaxNameLength);


                RuleFor(u => u.PrjName).NotEmpty().WithMessage(InspectionRecordMsg.NeedPrjName);
                Custom(u =>
                {
                    if (Guid.Empty == u.WarehouseID)
                    {
                        return new ValidationFailure("WarehouseID", InspectionRecordMsg.NeedWarehouseID);
                    }
                    return null;
                });

            });
        }
    }
}

